#################################################################
# @Description: 用例名称：支持CPU拓扑模拟和透传，测试目的：测试虚拟机支持 CPU 拓扑模拟和透传功能是否正常
# @Date : 2023-11-24
# @Last Modified by :
# @Last Modified time: 2023-11-27
#################################################################
source "../common.sh"
export LANG=en_US.UTF-8
res=0
vm10_name="vm10"
vm10_xml="/opt/${vm10_name}.xml"
cleanup_environment > /dev/null 2>&1

echo -e "\n$vm10_xml 相关配置如下："
cat $vm10_xml|grep "<vcpu placement='static'>4</vcpu>" || res=1
cat $vm10_xml|grep "<cpu mode='host-passthrough' check='none'>" || res=1
cat $vm10_xml|grep "<topology sockets='2' dies='1' cores='2' threads='1'/>" || res=1
echo -e "\n"
echo "创建并启动虚拟机vm10"
echo "开始执行命令：virsh define $vm10_name"
virsh define "$vm10_xml" > /dev/null 2>&1 || { res=1; echo "$vm10_name define failed"; exit 1; }
echo "开始执行命令：virsh start $vm10_name"
virsh start "$vm10_name" > /dev/null 2>&1 || { res=1; echo "$vm10_name start failed"; exit 1; }

vnc_port=$(virsh vncdisplay $vm10_name)
# 循环查询虚拟机的 IP 地址
echo "虚拟机$vm10_name启动中······"
while true; do
    ip_vm10=$(get_vm_ip "$vm10_name")
    # 如果有 IP 地址，打印信息并写入文件
    if [ -n "$ip_vm10" ]; then
        # 检查并 ping 的 IP 地址
        check_and_ping $ip_vm10 > /dev/null 2>&1 && echo "1. ping $ip_vm10 成功，虚拟机$vm10_name 启动正常！" || { res=1; echo "虚拟机$vm10_name 启动失败！"; exit 1; }
        break  # 退出循环
    fi
    sleep 10
done

echo "在虚拟机中使用lscpu命令查看cpu信息中，是否有4个CPU，socket的参数2与vm10.xml配置项对应。"
sshpass -p "$password" ssh -o StrictHostKeyChecking=no -q $ip_vm10 "lscpu" >cpu_info
cat cpu_info
cpu_no=$(cat cpu_info|grep "^CPU(s):"|awk '{print $2}')
socket_no=$(cat cpu_info|grep "^Socket(s):"|awk '{print $2}')
core_per_socket=$(cat cpu_info|grep "^Socket(s):"|awk -F: '{print $2}'|awk '{gsub(/^\s+|\s+$/, "");print}')
thread_per_core=$(cat cpu_info|grep "^Thread(s) per core:"|awk -F: '{print $2}'|awk '{gsub(/^\s+|\s+$/, "");print}')

[ "$cpu_no" == "4" ] || res=1
[ "$socket_no" == "2" ] || res=1
[ "$core_per_socket" == "2" ] || res=1
[ "$thread_per_core" == "1" ] || res=1

cleanup_environment > /dev/null 2>&1
# 判断用例是否执行成功
[ "$res" = "0" ] && echo -e "\033[32m-----------------test PASSED----------------\033[0m" || echo -e "\033[31m-----------------test FAILED----------------\033[0m"
